﻿using Entities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Utils;
using VO.User;

namespace Bridges
{
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class User
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public async Task<string> Login(string data)
        {
            LoginVO vo = JsonConvert.DeserializeObject<LoginVO>(data);
            UserEntity user = await SqlSugarHelper.Db.Queryable<UserEntity>()
                .Where(um => um.IsActive == 1)
                .Where(um => um.UserName == vo.UserName)
                .SingleAsync();
            if (user == null)
            {
                return JsonConvert.SerializeObject(new {
                    StatusCode = -1,
                    Succeeded = false,
                    Data = "",
                    Errors = "该账户不存在",
                    Extras = new { },
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }

            if (!user.Password.Equals(vo.Password))
            {
                return JsonConvert.SerializeObject(new {
                    StatusCode = -1,
                    Succeeded = false,
                    Data = "",
                    Errors = "密码错误",
                    Extras = new { },
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }

            return JsonConvert.SerializeObject(new {
                StatusCode = 200,
                Succeeded = true,
                Data = user,
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }
    }
}
